package luke4fun.wheresmymoney.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import luke4fun.wheresmymoney.AddBudgetActivity;
import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.impl.BudgetInfoListAdapter;
import luke4fun.wheresmymoney.service.async.BudgetInfoAsyncTask;

public class BudgetFragment extends Fragment {

	private Context context;
	private ExpandableListView budgetList;
	private BudgetInfoListAdapter adapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		context = getActivity();
		// init views
		View v = inflater.inflate(R.layout.fragment_budget, container, false);
		initView(v);

		setHasOptionsMenu(true);
		return v;
	}
	
	@Override
	public void onResume() {
		super.onResume();
        getActivity().setTitle(getString(R.string.title_budget));
		new BudgetInfoAsyncTask(context, adapter).execute();
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.menu_add, menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_add_new) {
            Intent intent = new Intent(getActivity(), AddBudgetActivity.class);
            startActivity(intent);
            return true;
        }

        return false;
	}
	
	private void initView(View v) {
		budgetList = (ExpandableListView) v.findViewById(R.id.budgets);
		adapter = new BudgetInfoListAdapter(context);
		budgetList.setAdapter(adapter);
	}
}
